package com.zdkj.subway.stm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.subway.stm.constant.StmTableCommon;
import com.zdkj.subway.stm.entity.SwStmStationDataEntryLog;
import com.zdkj.subway.stm.mapper.SwStmStationDataEntryLogMapper;
import com.zdkj.subway.stm.service.SwStmStationDataEntryLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SwStmStationDataEntryLogServiceImpl extends ServiceImpl<SwStmStationDataEntryLogMapper, SwStmStationDataEntryLog> implements SwStmStationDataEntryLogService {

    /**
     * 钱箱清点记录查询
     * @param id
     * @return
     */
    @Override
    public Map<String, List<Object>> mapListTvmCheckDetails(Integer id) {
        Map<String,List<Object>> listMap = new HashMap<>();
        List<SwStmStationDataEntryLog> swStmStationDataEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                        .eq(SwStmStationDataEntryLog::getId, id)
                        .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TVM_CHECK_DETAIL));

        if(CollectionUtil.isNotEmpty(swStmStationDataEntryLogs)){

            List<SwStmStationDataEntryLog> coinEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TVM_CHECK_DETAIL_COIN));

            List<SwStmStationDataEntryLog> coinChangeEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TVM_CHECK_DETAIL_COIN_CHANGE));

            List<SwStmStationDataEntryLog> paperEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TVM_CHECK_DETAIL_PAPER));

            List<SwStmStationDataEntryLog> paperChangeEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TVM_CHECK_DETAIL_PAPER_CHANGE));

            List<SwStmStationDataEntryLog> wasteEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TVM_CHECK_DETAIL_WASTE));

            /**
             * 主表存map
             */
            List<Object> LogInfoMainList = new ArrayList<>();
            swStmStationDataEntryLogs.stream().forEach(x -> {
                JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                LogInfoMainList.add(jsonObject);
            });
            listMap.put(StmTableCommon.SW_ST_TVM_CHECK_DETAIL,LogInfoMainList);

            //返回子表json串map集合
            if(CollectionUtil.isNotEmpty(coinEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                coinEntryLogs.stream().forEach(x -> {
                    JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                    LogInfoList.add(jsonObject);
                });
                //List<Object> coinList = coinEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TVM_CHECK_DETAIL_COIN,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(coinChangeEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                coinChangeEntryLogs.stream().forEach(x -> {
                    JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                    LogInfoList.add(jsonObject);
                });
                //List<Object> coinChangeList = coinChangeEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TVM_CHECK_DETAIL_COIN_CHANGE,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(paperEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                paperEntryLogs.stream().forEach(x -> {
                    JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                    LogInfoList.add(jsonObject);
                });
                //List<Object> paperList = paperEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TVM_CHECK_DETAIL_PAPER,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(paperChangeEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                paperChangeEntryLogs.stream().forEach(x -> {
                    JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                    LogInfoList.add(jsonObject);
                });
                //List<Object> paperChangeList = paperChangeEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TVM_CHECK_DETAIL_PAPER_CHANGE,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(wasteEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                wasteEntryLogs.stream().forEach(x -> {
                    JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                    LogInfoList.add(jsonObject);
                });
                //List<Object> wasteList = wasteEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TVM_CHECK_DETAIL_WASTE,LogInfoList);
            }
        }
        return listMap;
    }

    /**
     * 营销车票录入记录查询
     * @param id
     * @return
     */
    @Override
    public Map<String, List<Object>> mapListMarketingTicket(Integer id) {
        Map<String,List<Object>> listMap = new HashMap<>();
        List<SwStmStationDataEntryLog> swStmStationDataEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                .eq(SwStmStationDataEntryLog::getId, id)
                .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_MARKETING_TICKET));

        if(CollectionUtil.isNotEmpty(swStmStationDataEntryLogs)){

            List<SwStmStationDataEntryLog> groupSellEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_MARKETING_TICKET_GROUP_SELL));

            List<SwStmStationDataEntryLog> offineSellEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_MARKETING_TICKET_OFFINE_SELL));

            List<SwStmStationDataEntryLog> offineTakeTicketEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_MARKETING_TICKET_OFFINE_TAKE_TICKET));


            /**
             * 主表存map
             */
            List<Object> LogInfoMainList = new ArrayList<>();
            swStmStationDataEntryLogs.stream().forEach(x -> {
                JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                LogInfoMainList.add(jsonObject);
            });
            listMap.put(StmTableCommon.SW_ST_MARKETING_TICKET,LogInfoMainList);

            //返回子表json串map集合
            if(CollectionUtil.isNotEmpty(groupSellEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                groupSellEntryLogs.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> groupSellList = groupSellEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_MARKETING_TICKET_GROUP_SELL,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(offineSellEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                offineSellEntryLogs.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> offineSellList = offineSellEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_MARKETING_TICKET_OFFINE_SELL,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(offineTakeTicketEntryLogs)){
                List<Object> LogInfoList = new ArrayList<>();
                offineTakeTicketEntryLogs.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> offineTakeTicketList = offineTakeTicketEntryLogs.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_MARKETING_TICKET_OFFINE_TAKE_TICKET,LogInfoList);
            }
        }
        return listMap;
    }

    /**
     * 车票与配备用金
     * @param id
     * @return
     */
    @Override
    public Map<String, List<Object>> mapListTicketPettyAdd(Integer id) {
        Map<String,List<Object>> listMap = new HashMap<>();
        List<SwStmStationDataEntryLog> swStmStationDataEntryLogs = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                .eq(SwStmStationDataEntryLog::getId, id)
                .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD));

        if(CollectionUtil.isNotEmpty(swStmStationDataEntryLogs)){

            //首次配备用金
            List<SwStmStationDataEntryLog> cashDetailsEntryLogs0 = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_0));

            //新增配备用金
            List<SwStmStationDataEntryLog> cashDetailsEntryLogs1 = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_1));

            //归还配备用金
            List<SwStmStationDataEntryLog> cashDetailsEntryLogs2 = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_2));

            //首次配票
            List<SwStmStationDataEntryLog> addDetailsEntryLogs0 = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD_DETAILS_0));

            //新增配票
            List<SwStmStationDataEntryLog> addDetailsEntryLogs1 = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD_DETAILS_1));

            //归还配票
            List<SwStmStationDataEntryLog> addDetailsEntryLogs2 = this.baseMapper.selectList(Wrappers.<SwStmStationDataEntryLog>lambdaQuery()
                    .eq(SwStmStationDataEntryLog::getMainId,id)
                    .eq(SwStmStationDataEntryLog::getLogTableName, StmTableCommon.SW_ST_TICKET_PETTY_ADD_DETAILS_2));


            /**
             * 主表存map
             */
            List<Object> LogInfoMainList = new ArrayList<>();
            swStmStationDataEntryLogs.stream().forEach(x -> {
                JSONObject jsonObject = JSONObject.parseObject(x.getLogInfo());
                LogInfoMainList.add(jsonObject);
            });
            listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD,LogInfoMainList);

            //返回子表json串map集合
            if(CollectionUtil.isNotEmpty(cashDetailsEntryLogs0)){
                List<Object> LogInfoList = new ArrayList<>();
                cashDetailsEntryLogs0.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> cashDetailsList0 = cashDetailsEntryLogs0.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_0,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(cashDetailsEntryLogs1)){
                List<Object> LogInfoList = new ArrayList<>();
                cashDetailsEntryLogs1.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> cashDetailsList1 = cashDetailsEntryLogs1.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_1,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(cashDetailsEntryLogs2)){
                List<Object> LogInfoList = new ArrayList<>();
                cashDetailsEntryLogs2.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> cashDetailsList1 = cashDetailsEntryLogs1.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD_CASH_DETAILS_2,LogInfoList);
            }

            if(CollectionUtil.isNotEmpty(addDetailsEntryLogs0)){
                List<Object> LogInfoList = new ArrayList<>();
                addDetailsEntryLogs0.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> addDetailsList0 = addDetailsEntryLogs0.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD_DETAILS_0,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(addDetailsEntryLogs1)){
                List<Object> LogInfoList = new ArrayList<>();
                addDetailsEntryLogs1.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> addDetailsList1 = addDetailsEntryLogs0.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD_DETAILS_1,LogInfoList);
            }
            if(CollectionUtil.isNotEmpty(addDetailsEntryLogs2)){
                List<Object> LogInfoList = new ArrayList<>();
                addDetailsEntryLogs2.stream().forEach(x -> {
                    JSONArray jsonArray = JSONArray.parseArray(x.getLogInfo()); // 首先把字符串转成 JSONArray  对象
                    if(jsonArray.size()>0){
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            LogInfoList.add(jsonObject);
                        }
                    }
                });
                //List<Object> addDetailsList1 = addDetailsEntryLogs0.stream().map(SwStmStationDataEntryLog::getLogInfo).collect(Collectors.toList());
                listMap.put(StmTableCommon.SW_ST_TICKET_PETTY_ADD_DETAILS_2,LogInfoList);
            }
        }
        return listMap;
    }
}
